package com.nimitz.module.bi.service.impl;

import com.nimitz.common.utils.StringUtils;
import com.nimitz.module.bi.domain.BiEmployee;
import com.nimitz.module.bi.mapper.BiEmployeeMapper;
import com.nimitz.module.bi.service.IBiEmployeeService;
import com.nimitz.module.employee.domain.EmpEmployee;
import com.nimitz.module.employee.mapper.EmpEmployeeMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * 人员报表 业务层实现
 */
@Service
public class BiEmployeeServiceImpl implements IBiEmployeeService {

    private static final Logger logger = LoggerFactory.getLogger(BiEmployeeServiceImpl.class);

    @Resource
    private BiEmployeeMapper biEmployeeMapper;
    @Resource
    private EmpEmployeeMapper empEmployeeMapper;

    /**
     * 生成人员报表
     * 部门id的父节点为非跟节点时，只保留父节点求和记录，子节点数据不进行记录，并产生一条跟节点数据。
     * Step.1各部门统计自己的数据，Step.2 变更子部门id为父部门id，Step.3子部门数据求和后产生一条记录，Step.4所有子部门求和再产生一条根部门记录
     *
     * @param statistics 统计日期
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int generateEmployee(String statistics) {
        if (StringUtils.isEmpty(statistics)) {
            return 0;
        }
        // 线程延时队列-开始
        ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
        executorService.schedule(() -> {
            try {
                // 查询人员基本信息表所有未删除的数据
                List<EmpEmployee> empEmployeeList = empEmployeeMapper.selectEmployeeList(new EmpEmployee());
                if (CollectionUtils.isNotEmpty(empEmployeeList)) {
                    // 过滤待入职人员
                    empEmployeeList = empEmployeeList.stream().filter(employee -> !Objects.equals(employee.getStatus(), "0")).collect(Collectors.toList());
                    logger.info("生成人员报表" + empEmployeeList);
                    // 按照部门分组后的人员Map
                    Map<Integer, List<EmpEmployee>> empEmployeeMap = empEmployeeList.stream()
                            .collect(Collectors.groupingBy(EmpEmployee::getDeptId));
                    // 部门Map<子节点，父节点>
                    Map<Integer, Integer> deptMap = new HashMap<>();
                    generateDeptMap(empEmployeeMap, deptMap);
                    logger.info("生成人员报表" + deptMap);
                    // 各部门统计数据的集合
                    List<BiEmployee> biEmployeeALL = new ArrayList<>();
                    generateBiEmployeeAll(statistics, biEmployeeALL, empEmployeeMap);
                    logger.info("生成人员报表" + biEmployeeALL);
                    // 部门id子节点变更为父节点id集合
                    List<BiEmployee> changeDeptIdList = new ArrayList<>();
                    // 父节点为1时，直接进行保存，当父节点不为1时，变更子部门id为父节点id
                    changeDeptId(biEmployeeALL, deptMap, changeDeptIdList);
                    logger.info("生成人员报表" + changeDeptIdList);
                    // 子部门求和后集合
                    Map<Integer, List<BiEmployee>> biEmployeeSumList = changeDeptIdList.stream()
                            .collect(Collectors.groupingBy(BiEmployee::getDeptId));
                    // 节点变更后求和及根节点记录集合
                    List<BiEmployee> biEmployeeList = new ArrayList<>();
                    // 子节点求和
                    sumChildNode(statistics, biEmployeeSumList, biEmployeeList);
                    // 根节点求和
                    sumRootNode(statistics, biEmployeeList);
                    logger.info("生成人员报表" + biEmployeeList);
                    // 删除所有已生成记录
                    biEmployeeMapper.deleteEmployeeALL("admin");
                    // 批量添加
                    biEmployeeMapper.insertEmployee(biEmployeeList);

                }
            } catch (Exception e) {
                logger.info("生成人员报表" + e.getMessage());
                e.printStackTrace();
            } finally {
            }
        }, 0, TimeUnit.SECONDS);
        // 线程延时队列-结束
        return 1;
    }

    /**
     * 根据部门id查询人员
     *
     * @param deptId 部门id
     * @return 人员
     */
    @Override
    public BiEmployee selectEmployeeByDeptId(Integer deptId) {
        return biEmployeeMapper.selectEmployeeByDeptId(deptId);
    }

    /**
     * 生成部门Map<子节点，父节点>
     *
     * @param empEmployeeMap 按照部门分组后的人员Map
     * @param deptMap        部门Map
     */
    private void generateDeptMap(Map<Integer, List<EmpEmployee>> empEmployeeMap, Map<Integer, Integer> deptMap) {
        for (Entry<Integer, List<EmpEmployee>> entry : empEmployeeMap.entrySet()) {
            for (EmpEmployee employee : entry.getValue()) {
                deptMap.put(entry.getKey(), employee.getEmployeeDepartment().getParentId().intValue());
            }
        }
    }

    /**
     * 生成所有部门的集合
     *
     * @param statistics     统计日期
     * @param biEmployeeALL  各部门统计数据的集合
     * @param empEmployeeMap 按照部门分组后的人员Map
     */
    private void generateBiEmployeeAll(String statistics, List<BiEmployee> biEmployeeALL, Map<Integer, List<EmpEmployee>> empEmployeeMap) {
        for (Entry<Integer, List<EmpEmployee>> entry : empEmployeeMap.entrySet()) {
            BiEmployee biEmployee = new BiEmployee();
            biEmployee.setEmployeeStatistics(statistics);
            biEmployee.setDeptId(entry.getKey());
            // 按在职状态进行分组
            Map<String, List<EmpEmployee>> employeeMap = entry.getValue().stream()
                    .collect(Collectors.groupingBy(EmpEmployee::getStatus));
            // 在职人员集合
            List<EmpEmployee> empEmployeeList1 = employeeMap.get("1");
            // 离职人员集合
            List<EmpEmployee> empEmployeeList2 = employeeMap.get("2");
            // 人数
            generateNumber(biEmployee, empEmployeeList1, empEmployeeList2);
            // 类型
            generateType(biEmployee, empEmployeeList1, empEmployeeList2);
            // 学历
            generateEducation(biEmployee, empEmployeeList1);
            // 层级
            generateLevel(biEmployee, empEmployeeList1);
            // 机构
            generateAgency(biEmployee, empEmployeeList1);
            biEmployeeALL.add(biEmployee);
        }
    }

    /**
     * 生成人数相关
     *
     * @param biEmployee       待赋值的报表对象
     * @param empEmployeeList1 在职集合
     * @param empEmployeeList2 离职集合
     * @return 已赋值的报表对象
     */
    private void generateNumber(BiEmployee biEmployee, List<EmpEmployee> empEmployeeList1, List<EmpEmployee> empEmployeeList2) {
        Integer total = 0;
        Integer male = 0;
        Integer female = 0;
        Integer totalLeave = 0;
        Integer maleLeave = 0;
        Integer femaleLeave = 0;
        if (CollectionUtils.isNotEmpty(empEmployeeList1)) {
            // 在职相关
            total = empEmployeeList1.size();
            // 按性别分组
            Map<String, List<EmpEmployee>> empEmployeeMap1 = empEmployeeList1.stream()
                    .filter(employee -> employee.getEmployeeSex() != null)
                    .collect(Collectors.groupingBy(EmpEmployee::getEmployeeSex));
            male = empEmployeeMap1.get("0") != null ? empEmployeeMap1.get("0").size() : 0;
            female = empEmployeeMap1.get("1") != null ? empEmployeeMap1.get("1").size() : 0;
        }
        if (CollectionUtils.isNotEmpty(empEmployeeList2)) {
            // 离职相关
            totalLeave = empEmployeeList2.size();
            // 按性别分组
            Map<String, List<EmpEmployee>> empEmployeeMap2 = empEmployeeList2.stream()
                    .filter(employee -> employee.getEmployeeSex() != null)
                    .collect(Collectors.groupingBy(EmpEmployee::getEmployeeSex));
            maleLeave = empEmployeeMap2.get("0") != null ? empEmployeeMap2.get("0").size() : 0;
            femaleLeave = empEmployeeMap2.get("1") != null ? empEmployeeMap2.get("1").size() : 0;
        }
        biEmployee.setEmployeeTotal(total);
        biEmployee.setEmployeeMale(male);
        biEmployee.setEmployeeFemale(female);
        biEmployee.setEmployeeTotalLeave(totalLeave);
        biEmployee.setEmployeeMaleLeave(maleLeave);
        biEmployee.setEmployeeFemaleLeave(femaleLeave);
    }

    /**
     * 生成类型相关
     *
     * @param biEmployee       待赋值的报表对象
     * @param empEmployeeList1 在职集合
     * @param empEmployeeList2 离职集合
     * @return 已赋值的报表对象
     */
    private void generateType(BiEmployee biEmployee, List<EmpEmployee> empEmployeeList1, List<EmpEmployee> empEmployeeList2) {
        Integer civil = 0;
        Integer military = 0;
        Integer civilLeave = 0;
        Integer militaryLeave = 0;
        if (CollectionUtils.isNotEmpty(empEmployeeList1)) {
            // 在职按类型分组
            Map<String, List<EmpEmployee>> empEmployeeMap1 = empEmployeeList1.stream()
                    .filter(employee -> employee.getEmployeeType() != null)
                    .collect(Collectors.groupingBy(EmpEmployee::getEmployeeType));
            civil = empEmployeeMap1.get("0") != null ? empEmployeeMap1.get("0").size() : 0;
            military = empEmployeeMap1.get("1") != null ? empEmployeeMap1.get("1").size() : 0;
        }
        if (CollectionUtils.isNotEmpty(empEmployeeList2)) {
            // 离职按类型分组
            Map<String, List<EmpEmployee>> empEmployeeMap2 = empEmployeeList2.stream()
                    .filter(employee -> employee.getEmployeeType() != null)
                    .collect(Collectors.groupingBy(EmpEmployee::getEmployeeType));
            civilLeave = empEmployeeMap2.get("0") != null ? empEmployeeMap2.get("0").size() : 0;
            militaryLeave = empEmployeeMap2.get("1") != null ? empEmployeeMap2.get("1").size() : 0;
        }
        biEmployee.setEmployeeCivil(civil);
        biEmployee.setEmployeeMilitary(military);
        biEmployee.setEmployeeCivilLeave(civilLeave);
        biEmployee.setEmployeeMilitaryLeave(militaryLeave);
    }

    /**
     * 生成学历相关
     *
     * @param biEmployee       待赋值的报表对象
     * @param empEmployeeList1 在职集合
     * @return 已赋值的报表对象
     */
    private void generateEducation(BiEmployee biEmployee, List<EmpEmployee> empEmployeeList1) {
        Integer senior = 0;
        Integer junior = 0;
        Integer college = 0;
        Integer master = 0;
        if (CollectionUtils.isNotEmpty(empEmployeeList1)) {
            // 在职按学历分组
            Map<String, List<EmpEmployee>> empEmployeeMap1 = empEmployeeList1.stream()
                    .filter(employee -> employee.getEmployeeEducation() != null)
                    .collect(Collectors.groupingBy(EmpEmployee::getEmployeeEducation));
            senior = empEmployeeMap1.get("0") != null ? empEmployeeMap1.get("0").size() : 0;
            junior = empEmployeeMap1.get("1") != null ? empEmployeeMap1.get("1").size() : 0;
            college = empEmployeeMap1.get("2") != null ? empEmployeeMap1.get("2").size() : 0;
            master = empEmployeeMap1.get("3") != null ? empEmployeeMap1.get("3").size() : 0;
            master += empEmployeeMap1.get("4") != null ? empEmployeeMap1.get("4").size() : 0;
        }
        biEmployee.setEmployeeSenior(senior);
        biEmployee.setEmployeeJunior(junior);
        biEmployee.setEmployeeCollege(college);
        biEmployee.setEmployeeMaster(master);
    }

    /**
     * 生成层级相关
     *
     * @param biEmployee       待赋值的报表对象
     * @param empEmployeeList1 在职集合
     * @return 已赋值的报表对象
     */
    private void generateLevel(BiEmployee biEmployee, List<EmpEmployee> empEmployeeList1) {
        Integer level1 = 0, level2 = 0, level3 = 0, level4 = 0, level5 = 0, level6 = 0, level7 = 0;
        Integer cLevel1 = 0, cLevel2 = 0, cLevel3 = 0, cLevel4 = 0, cLevel5 = 0, cLevel6 = 0, cLevel7 = 0;
        Integer mLevel1 = 0, mLevel2 = 0, mLevel3 = 0, mLevel4 = 0, mLevel5 = 0, mLevel6 = 0, mLevel7 = 0;
        if (CollectionUtils.isNotEmpty(empEmployeeList1)) {
            // 在职按层级分组
            Map<String, List<EmpEmployee>> empEmployeeMap1 = empEmployeeList1.stream()
                    .filter(employee -> employee.getEmployeeLevel() != null)
                    .collect(Collectors.groupingBy(EmpEmployee::getEmployeeLevel));
            level1 = empEmployeeMap1.get("0") != null ? empEmployeeMap1.get("0").size() : 0;
            level2 = empEmployeeMap1.get("1") != null ? empEmployeeMap1.get("1").size() : 0;
            level3 = empEmployeeMap1.get("2") != null ? empEmployeeMap1.get("2").size() : 0;
            level4 = empEmployeeMap1.get("3") != null ? empEmployeeMap1.get("3").size() : 0;
            level5 = empEmployeeMap1.get("4") != null ? empEmployeeMap1.get("4").size() : 0;
            level6 = empEmployeeMap1.get("5") != null ? empEmployeeMap1.get("5").size() : 0;
            level7 = empEmployeeMap1.get("6") != null ? empEmployeeMap1.get("6").size() : 0;

            // 在职按类型按层级分组
            Map<String, Map<String, List<EmpEmployee>>> empEmployeeMap2 = empEmployeeList1.stream()
                    .filter(employee -> employee.getEmployeeType() != null && employee.getEmployeeLevel() != null)
                    .collect(Collectors.groupingBy(EmpEmployee::getEmployeeType,
                            Collectors.groupingBy(EmpEmployee::getEmployeeLevel)
                    ));
            // 文职
            cLevel1 = empEmployeeMap2.get("0") != null ? (empEmployeeMap2.get("0").get("0") != null ? empEmployeeMap2.get("0").get("0").size() : 0) : 0;
            cLevel2 = empEmployeeMap2.get("0") != null ? (empEmployeeMap2.get("0").get("1") != null ? empEmployeeMap2.get("0").get("1").size() : 0) : 0;
            cLevel3 = empEmployeeMap2.get("0") != null ? (empEmployeeMap2.get("0").get("2") != null ? empEmployeeMap2.get("0").get("2").size() : 0) : 0;
            cLevel4 = empEmployeeMap2.get("0") != null ? (empEmployeeMap2.get("0").get("3") != null ? empEmployeeMap2.get("0").get("3").size() : 0) : 0;
            cLevel5 = empEmployeeMap2.get("0") != null ? (empEmployeeMap2.get("0").get("4") != null ? empEmployeeMap2.get("0").get("4").size() : 0) : 0;
            cLevel6 = empEmployeeMap2.get("0") != null ? (empEmployeeMap2.get("0").get("5") != null ? empEmployeeMap2.get("0").get("5").size() : 0) : 0;
            cLevel7 = empEmployeeMap2.get("0") != null ? (empEmployeeMap2.get("0").get("6") != null ? empEmployeeMap2.get("0").get("6").size() : 0) : 0;
            // 勤务
            mLevel1 = empEmployeeMap2.get("1") != null ? (empEmployeeMap2.get("1").get("0") != null ? empEmployeeMap2.get("1").get("0").size() : 0) : 0;
            mLevel2 = empEmployeeMap2.get("1") != null ? (empEmployeeMap2.get("1").get("1") != null ? empEmployeeMap2.get("1").get("1").size() : 0) : 0;
            mLevel3 = empEmployeeMap2.get("1") != null ? (empEmployeeMap2.get("1").get("2") != null ? empEmployeeMap2.get("1").get("2").size() : 0) : 0;
            mLevel4 = empEmployeeMap2.get("1") != null ? (empEmployeeMap2.get("1").get("3") != null ? empEmployeeMap2.get("1").get("3").size() : 0) : 0;
            mLevel5 = empEmployeeMap2.get("1") != null ? (empEmployeeMap2.get("1").get("4") != null ? empEmployeeMap2.get("1").get("4").size() : 0) : 0;
            mLevel6 = empEmployeeMap2.get("1") != null ? (empEmployeeMap2.get("1").get("5") != null ? empEmployeeMap2.get("1").get("5").size() : 0) : 0;
            mLevel7 = empEmployeeMap2.get("1") != null ? (empEmployeeMap2.get("1").get("6") != null ? empEmployeeMap2.get("1").get("6").size() : 0) : 0;

        }
        biEmployee.setEmployeeLevel1(level1);
        biEmployee.setEmployeeLevel2(level2);
        biEmployee.setEmployeeLevel3(level3);
        biEmployee.setEmployeeLevel4(level4);
        biEmployee.setEmployeeLevel5(level5);
        biEmployee.setEmployeeLevel6(level6);
        biEmployee.setEmployeeLevel7(level7);

        biEmployee.setEmployeeCLevel1(cLevel1);
        biEmployee.setEmployeeCLevel2(cLevel2);
        biEmployee.setEmployeeCLevel3(cLevel3);
        biEmployee.setEmployeeCLevel4(cLevel4);
        biEmployee.setEmployeeCLevel5(cLevel5);
        biEmployee.setEmployeeCLevel6(cLevel6);
        biEmployee.setEmployeeCLevel7(cLevel7);

        biEmployee.setEmployeeMLevel1(mLevel1);
        biEmployee.setEmployeeMLevel2(mLevel2);
        biEmployee.setEmployeeMLevel3(mLevel3);
        biEmployee.setEmployeeMLevel4(mLevel4);
        biEmployee.setEmployeeMLevel5(mLevel5);
        biEmployee.setEmployeeMLevel6(mLevel6);
        biEmployee.setEmployeeMLevel7(mLevel7);
    }

    /**
     * 生成机构相关
     *
     * @param biEmployee       待赋值的报表对象
     * @param empEmployeeList1 在职集合
     * @return 已赋值的报表对象
     */
    private void generateAgency(BiEmployee biEmployee, List<EmpEmployee> empEmployeeList1) {
        Integer agency1 = 0;
        Integer agency2 = 0;
        Integer agency3 = 0;
        Integer agency4 = 0;
        Integer agency5 = 0;
        Integer agency6 = 0;
        Integer agency7 = 0;
        Integer agency8 = 0;
        Integer agency9 = 0;
        if (CollectionUtils.isNotEmpty(empEmployeeList1)) {
            // 在职按机构分组
            Map<String, List<EmpEmployee>> empEmployeeMap1 = empEmployeeList1.stream()
                    .filter(employee -> employee.getEmployeeAgency() != null)
                    .collect(Collectors.groupingBy(EmpEmployee::getEmployeeAgency));
            agency1 = empEmployeeMap1.get("0") != null ? empEmployeeMap1.get("0").size() : 0;
            agency2 = empEmployeeMap1.get("1") != null ? empEmployeeMap1.get("1").size() : 0;
            agency3 = empEmployeeMap1.get("2") != null ? empEmployeeMap1.get("2").size() : 0;
            agency4 = empEmployeeMap1.get("3") != null ? empEmployeeMap1.get("3").size() : 0;
            agency5 = empEmployeeMap1.get("4") != null ? empEmployeeMap1.get("4").size() : 0;
            agency6 = empEmployeeMap1.get("5") != null ? empEmployeeMap1.get("5").size() : 0;
            agency7 = empEmployeeMap1.get("6") != null ? empEmployeeMap1.get("6").size() : 0;
            agency8 = empEmployeeMap1.get("7") != null ? empEmployeeMap1.get("7").size() : 0;
            agency9 = empEmployeeMap1.get("8") != null ? empEmployeeMap1.get("8").size() : 0;
        }
        biEmployee.setEmployeeAgency1(agency1);
        biEmployee.setEmployeeAgency2(agency2);
        biEmployee.setEmployeeAgency3(agency3);
        biEmployee.setEmployeeAgency4(agency4);
        biEmployee.setEmployeeAgency5(agency5);
        biEmployee.setEmployeeAgency6(agency6);
        biEmployee.setEmployeeAgency7(agency7);
        biEmployee.setEmployeeAgency8(agency8);
        biEmployee.setEmployeeAgency9(agency9);
    }

    /**
     * 变更子部门id为父节点id（当父节点为1时，不进行变更）
     *
     * @param biEmployeeALL    各部门统计数据的集合
     * @param deptMap          部门Map
     * @param changeDeptIdList 变更子部门id后的集合
     */
    private void changeDeptId(List<BiEmployee> biEmployeeALL, Map<Integer, Integer> deptMap, List<BiEmployee> changeDeptIdList) {
        for (BiEmployee biEmployee : biEmployeeALL) {
            // 根据子部门查找父部门id
            Integer value = deptMap.get(biEmployee.getDeptId());
            if (value != 1) {
                biEmployee.setDeptId(value);
            }
            changeDeptIdList.add(biEmployee);
        }
    }

    /**
     * 子节点求和(按照部门id进行再次分组后求和)
     *
     * @param statistics        统计日期
     * @param biEmployeeSumList 子部门求和后Map
     * @param biEmployeeList    节点变更后求和及根节点记录集合
     */
    private void sumChildNode(String statistics, Map<Integer, List<BiEmployee>> biEmployeeSumList, List<BiEmployee> biEmployeeList) {
        biEmployeeSumList.forEach((deptId, biEmployees) -> {
            BiEmployee biEmployee = new BiEmployee();
            biEmployee.setEmployeeStatistics(statistics);
            biEmployee.setDeptId(deptId);
            biEmployee.setEmployeeTotal(biEmployees.stream().mapToInt(BiEmployee::getEmployeeTotal).sum());
            biEmployee.setEmployeeMale(biEmployees.stream().mapToInt(BiEmployee::getEmployeeMale).sum());
            biEmployee.setEmployeeFemale(biEmployees.stream().mapToInt(BiEmployee::getEmployeeFemale).sum());
            biEmployee.setEmployeeTotalLeave(biEmployees.stream().mapToInt(BiEmployee::getEmployeeTotalLeave).sum());
            biEmployee.setEmployeeMaleLeave(biEmployees.stream().mapToInt(BiEmployee::getEmployeeMaleLeave).sum());
            biEmployee.setEmployeeFemaleLeave(biEmployees.stream().mapToInt(BiEmployee::getEmployeeFemaleLeave).sum());
            biEmployee.setEmployeeCivil(biEmployees.stream().mapToInt(BiEmployee::getEmployeeCivil).sum());
            biEmployee.setEmployeeMilitary(biEmployees.stream().mapToInt(BiEmployee::getEmployeeMilitary).sum());
            biEmployee.setEmployeeCivilLeave(biEmployees.stream().mapToInt(BiEmployee::getEmployeeCivilLeave).sum());
            biEmployee.setEmployeeMilitaryLeave(biEmployees.stream().mapToInt(BiEmployee::getEmployeeMilitaryLeave).sum());
            biEmployee.setEmployeeSenior(biEmployees.stream().mapToInt(BiEmployee::getEmployeeSenior).sum());
            biEmployee.setEmployeeJunior(biEmployees.stream().mapToInt(BiEmployee::getEmployeeJunior).sum());
            biEmployee.setEmployeeCollege(biEmployees.stream().mapToInt(BiEmployee::getEmployeeCollege).sum());
            biEmployee.setEmployeeMaster(biEmployees.stream().mapToInt(BiEmployee::getEmployeeMaster).sum());
            biEmployee.setEmployeeLevel1(biEmployees.stream().mapToInt(BiEmployee::getEmployeeLevel1).sum());
            biEmployee.setEmployeeLevel2(biEmployees.stream().mapToInt(BiEmployee::getEmployeeLevel2).sum());
            biEmployee.setEmployeeLevel3(biEmployees.stream().mapToInt(BiEmployee::getEmployeeLevel3).sum());
            biEmployee.setEmployeeLevel4(biEmployees.stream().mapToInt(BiEmployee::getEmployeeLevel4).sum());
            biEmployee.setEmployeeLevel5(biEmployees.stream().mapToInt(BiEmployee::getEmployeeLevel5).sum());
            biEmployee.setEmployeeLevel6(biEmployees.stream().mapToInt(BiEmployee::getEmployeeLevel6).sum());
            biEmployee.setEmployeeLevel7(biEmployees.stream().mapToInt(BiEmployee::getEmployeeLevel7).sum());
            biEmployee.setEmployeeCLevel1(biEmployees.stream().mapToInt(BiEmployee::getEmployeeCLevel1).sum());
            biEmployee.setEmployeeCLevel2(biEmployees.stream().mapToInt(BiEmployee::getEmployeeCLevel2).sum());
            biEmployee.setEmployeeCLevel3(biEmployees.stream().mapToInt(BiEmployee::getEmployeeCLevel3).sum());
            biEmployee.setEmployeeCLevel4(biEmployees.stream().mapToInt(BiEmployee::getEmployeeCLevel4).sum());
            biEmployee.setEmployeeCLevel5(biEmployees.stream().mapToInt(BiEmployee::getEmployeeCLevel5).sum());
            biEmployee.setEmployeeCLevel6(biEmployees.stream().mapToInt(BiEmployee::getEmployeeCLevel6).sum());
            biEmployee.setEmployeeCLevel7(biEmployees.stream().mapToInt(BiEmployee::getEmployeeCLevel7).sum());
            biEmployee.setEmployeeMLevel1(biEmployees.stream().mapToInt(BiEmployee::getEmployeeMLevel1).sum());
            biEmployee.setEmployeeMLevel2(biEmployees.stream().mapToInt(BiEmployee::getEmployeeMLevel2).sum());
            biEmployee.setEmployeeMLevel3(biEmployees.stream().mapToInt(BiEmployee::getEmployeeMLevel3).sum());
            biEmployee.setEmployeeMLevel4(biEmployees.stream().mapToInt(BiEmployee::getEmployeeMLevel4).sum());
            biEmployee.setEmployeeMLevel5(biEmployees.stream().mapToInt(BiEmployee::getEmployeeMLevel5).sum());
            biEmployee.setEmployeeMLevel6(biEmployees.stream().mapToInt(BiEmployee::getEmployeeMLevel6).sum());
            biEmployee.setEmployeeMLevel7(biEmployees.stream().mapToInt(BiEmployee::getEmployeeMLevel7).sum());
            biEmployee.setEmployeeAgency1(biEmployees.stream().mapToInt(BiEmployee::getEmployeeAgency1).sum());
            biEmployee.setEmployeeAgency2(biEmployees.stream().mapToInt(BiEmployee::getEmployeeAgency2).sum());
            biEmployee.setEmployeeAgency3(biEmployees.stream().mapToInt(BiEmployee::getEmployeeAgency3).sum());
            biEmployee.setEmployeeAgency4(biEmployees.stream().mapToInt(BiEmployee::getEmployeeAgency4).sum());
            biEmployee.setEmployeeAgency5(biEmployees.stream().mapToInt(BiEmployee::getEmployeeAgency5).sum());
            biEmployee.setEmployeeAgency6(biEmployees.stream().mapToInt(BiEmployee::getEmployeeAgency6).sum());
            biEmployee.setEmployeeAgency7(biEmployees.stream().mapToInt(BiEmployee::getEmployeeAgency7).sum());
            biEmployee.setEmployeeAgency8(biEmployees.stream().mapToInt(BiEmployee::getEmployeeAgency8).sum());
            biEmployee.setEmployeeAgency9(biEmployees.stream().mapToInt(BiEmployee::getEmployeeAgency9).sum());
            biEmployeeList.add(biEmployee);
        });
    }

    /**
     * 根节点求和(将所有部门求和)
     *
     * @param statistics     统计日期
     * @param biEmployeeList 节点变更后求和及根节点记录集合
     */
    private void sumRootNode(String statistics, List<BiEmployee> biEmployeeList) {
        BiEmployee biEmployee = new BiEmployee();
        biEmployee.setEmployeeStatistics(statistics);
        biEmployee.setDeptId(1);
        biEmployee.setEmployeeTotal(biEmployeeList.stream().mapToInt(BiEmployee::getEmployeeTotal).sum());
        biEmployee.setEmployeeMale(biEmployeeList.stream().mapToInt(BiEmployee::getEmployeeMale).sum());
        biEmployee.setEmployeeFemale(biEmployeeList.stream().mapToInt(BiEmployee::getEmployeeFemale).sum());
        biEmployee.setEmployeeTotalLeave(biEmployeeList.stream().mapToInt(BiEmployee::getEmployeeTotalLeave).sum());
        biEmployee.setEmployeeMaleLeave(biEmployeeList.stream().mapToInt(BiEmployee::getEmployeeMaleLeave).sum());
        biEmployee.setEmployeeFemaleLeave(biEmployeeList.stream().mapToInt(BiEmployee::getEmployeeFemaleLeave).sum());
        biEmployee.setEmployeeCivil(biEmployeeList.stream().mapToInt(BiEmployee::getEmployeeCivil).sum());
        biEmployee.setEmployeeMilitary(biEmployeeList.stream().mapToInt(BiEmployee::getEmployeeMilitary).sum());
        biEmployee.setEmployeeCivilLeave(biEmployeeList.stream().mapToInt(BiEmployee::getEmployeeCivilLeave).sum());
        biEmployee.setEmployeeMilitaryLeave(biEmployeeList.stream().mapToInt(BiEmployee::getEmployeeMilitaryLeave).sum());
        biEmployee.setEmployeeSenior(biEmployeeList.stream().mapToInt(BiEmployee::getEmployeeSenior).sum());
        biEmployee.setEmployeeJunior(biEmployeeList.stream().mapToInt(BiEmployee::getEmployeeJunior).sum());
        biEmployee.setEmployeeCollege(biEmployeeList.stream().mapToInt(BiEmployee::getEmployeeCollege).sum());
        biEmployee.setEmployeeMaster(biEmployeeList.stream().mapToInt(BiEmployee::getEmployeeMaster).sum());
        biEmployee.setEmployeeLevel1(biEmployeeList.stream().mapToInt(BiEmployee::getEmployeeLevel1).sum());
        biEmployee.setEmployeeLevel2(biEmployeeList.stream().mapToInt(BiEmployee::getEmployeeLevel2).sum());
        biEmployee.setEmployeeLevel3(biEmployeeList.stream().mapToInt(BiEmployee::getEmployeeLevel3).sum());
        biEmployee.setEmployeeLevel4(biEmployeeList.stream().mapToInt(BiEmployee::getEmployeeLevel4).sum());
        biEmployee.setEmployeeLevel5(biEmployeeList.stream().mapToInt(BiEmployee::getEmployeeLevel5).sum());
        biEmployee.setEmployeeLevel6(biEmployeeList.stream().mapToInt(BiEmployee::getEmployeeLevel6).sum());
        biEmployee.setEmployeeLevel7(biEmployeeList.stream().mapToInt(BiEmployee::getEmployeeLevel7).sum());
        biEmployee.setEmployeeCLevel1(biEmployeeList.stream().mapToInt(BiEmployee::getEmployeeCLevel1).sum());
        biEmployee.setEmployeeCLevel2(biEmployeeList.stream().mapToInt(BiEmployee::getEmployeeCLevel2).sum());
        biEmployee.setEmployeeCLevel3(biEmployeeList.stream().mapToInt(BiEmployee::getEmployeeCLevel3).sum());
        biEmployee.setEmployeeCLevel4(biEmployeeList.stream().mapToInt(BiEmployee::getEmployeeCLevel4).sum());
        biEmployee.setEmployeeCLevel5(biEmployeeList.stream().mapToInt(BiEmployee::getEmployeeCLevel5).sum());
        biEmployee.setEmployeeCLevel6(biEmployeeList.stream().mapToInt(BiEmployee::getEmployeeCLevel6).sum());
        biEmployee.setEmployeeCLevel7(biEmployeeList.stream().mapToInt(BiEmployee::getEmployeeCLevel7).sum());
        biEmployee.setEmployeeMLevel1(biEmployeeList.stream().mapToInt(BiEmployee::getEmployeeMLevel1).sum());
        biEmployee.setEmployeeMLevel2(biEmployeeList.stream().mapToInt(BiEmployee::getEmployeeMLevel2).sum());
        biEmployee.setEmployeeMLevel3(biEmployeeList.stream().mapToInt(BiEmployee::getEmployeeMLevel3).sum());
        biEmployee.setEmployeeMLevel4(biEmployeeList.stream().mapToInt(BiEmployee::getEmployeeMLevel4).sum());
        biEmployee.setEmployeeMLevel5(biEmployeeList.stream().mapToInt(BiEmployee::getEmployeeMLevel5).sum());
        biEmployee.setEmployeeMLevel6(biEmployeeList.stream().mapToInt(BiEmployee::getEmployeeMLevel6).sum());
        biEmployee.setEmployeeMLevel7(biEmployeeList.stream().mapToInt(BiEmployee::getEmployeeMLevel7).sum());
        biEmployee.setEmployeeAgency1(biEmployeeList.stream().mapToInt(BiEmployee::getEmployeeAgency1).sum());
        biEmployee.setEmployeeAgency2(biEmployeeList.stream().mapToInt(BiEmployee::getEmployeeAgency2).sum());
        biEmployee.setEmployeeAgency3(biEmployeeList.stream().mapToInt(BiEmployee::getEmployeeAgency3).sum());
        biEmployee.setEmployeeAgency4(biEmployeeList.stream().mapToInt(BiEmployee::getEmployeeAgency4).sum());
        biEmployee.setEmployeeAgency5(biEmployeeList.stream().mapToInt(BiEmployee::getEmployeeAgency5).sum());
        biEmployee.setEmployeeAgency6(biEmployeeList.stream().mapToInt(BiEmployee::getEmployeeAgency6).sum());
        biEmployee.setEmployeeAgency7(biEmployeeList.stream().mapToInt(BiEmployee::getEmployeeAgency7).sum());
        biEmployee.setEmployeeAgency8(biEmployeeList.stream().mapToInt(BiEmployee::getEmployeeAgency8).sum());
        biEmployee.setEmployeeAgency9(biEmployeeList.stream().mapToInt(BiEmployee::getEmployeeAgency9).sum());
        biEmployeeList.add(biEmployee);
    }
}
